<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-warn"></i>&nbsp;&nbsp;一个管理员可以有多个角色组,左侧的菜单根据管理员所拥有的权限进行生成</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="table_btn">
                <el-button type="success" icon="el-icon-edit" @click="dialogFormVisible = true">添加</el-button>
                <el-button type="danger" icon="el-icon-delete"  @click="allDelete()">批量删除</el-button>
            </div>
            <div class="box_table">
                <el-table
                        :data="tableData"
                        border
                        @selection-change="handleSelectionChange">
                    style="width: 100%">
                    <el-table-column
                            type="selection"
                            width="55">
                    </el-table-column>
                    <el-table-column
                            prop="id"
                            label="序号"
                            width="80">
                    </el-table-column>
                    <el-table-column
                            prop="user"
                            label="用户名"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="nickname"
                            label="昵称"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="group"
                            label="所属组别">
                    </el-table-column>
                    <el-table-column
                            prop="email"
                            label="Email">
                    </el-table-column>
                    <el-table-column
                            prop="status"
                            label="状态"
                            width="100"
                            :filters="[{ text: '正常', value: '正常' }, { text: '禁用', value: '禁用' }]"
                            :filter-method="filterTag"
                            filter-placement="bottom-end">
                        <template slot-scope="scope">
                            <el-tag
                                    :type="scope.row.status === '正常' ? 'primary' : 'success'"
                                    disable-transitions>{{scope.row.status}}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="last_login"
                            label="最后登录">
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                    size="mini"
                                    icon="el-icon-edit"
                                    type="primary"
                                    @click="handleEdit(scope.$index, scope.row)">
                                编辑
                            </el-button>
                            <el-button
                                    size="mini"
                                    type="danger"
                                    icon="el-icon-delete"
                                    @click="handleDelete(scope.$index, scope.row)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!--添加-->
        <el-dialog title="添加管理员" :visible.sync="dialogFormVisible"  width="35%">
            <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                <el-form-item label="所属组别" prop="group">
                    <el-input v-model="form.group"></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="user">
                    <el-input v-model="form.user"></el-input>
                </el-form-item>
                <el-form-item label="昵称" prop="nickname">
                    <el-input v-model="form.nickname"></el-input>
                </el-form-item>
                <el-form-item label="email" prop="email">
                    <el-input v-model="form.email"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.status"
                               active-text="正常"
                               inactive-text="隐藏"
                               active-value="1"
                               inactive-value="0"
                    ></el-switch>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('form')">立即创建</el-button>
                    <el-button @click="resetForm('form')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>
<script>
    import { fetchData } from '../../../api/index';
    import axios from "axios";
    export default {
        data() {
            return {
                tableData: [{
                    id:'1',
                    user: 'admin',
                    nickname: 'admin',
                    group: '超级管理组',
                    email: 'admin123@163.com',
                    status: '正常',
                    last_login: '2019-12-26 10:28:02'
                }, {
                    id:'2',
                    user: 'admin',
                    nickname: 'admin',
                    group: '超级管理组',
                    email: 'admin123@163.com',
                    status: '正常',
                    last_login: '2019-12-26 10:28:02'
                }, {
                    id:'3',
                    user: 'admin',
                    nickname: 'admin',
                    group: '超级管理组',
                    email: 'admin123@163.com',
                    status: '禁用',
                    last_login: '2019-12-26 10:28:02'
                }, {
                    id:'4',
                    user: 'admin',
                    nickname: 'admin',
                    group: '超级管理组',
                    email: 'admin123@163.com',
                    status: '禁用',
                    last_login: '2019-12-26 10:28:02'
                }],
                query: {
                    address: '',
                    name: '',
                    pageIndex: 1,
                    pageSize: 10
                },
                pageTotal: 0,
                multipleSelection:[],
                dialogFormVisible:false,
                form: {
                    group: '',
                    user: '',
                    nickname: '',
                    email: '',
                    password: '',
                    status:'',

                },
                rules: {
                    group:[
                        { required: true, message: '请选择组别', trigger: 'change' }
                    ],
                    user: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    nickname: [
                        { required: true, message: '请输入昵称', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    email: [
                        { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                        { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' }
                    ],

                }

            }
        },
        mounted(){
            this.getData();

        },
        created() {
            this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                fetchData(this.query).then(res => {
                    console.log(res);
                    this.pageTotal = res.pageTotal || 50;
                });
            },
            clearFilter() {
                this.$refs.filterTable.clearFilter();
            },
            filterTag(value, row) {
                return row.status === value;
            },
            handleSelectionChange(val) {
                console.log(val);
                this.multipleSelection = val;
            },
            allDelete(){//批量删除
                if(this.multipleSelection.length<1){
                    this.$message.error('选择要删除的内容');
                    return ;
                }
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            submitForm(form) {//提交表单
                this.$refs[form].validate((valid) => {
                    if (valid) {
                        const  form = this.form;
                        console.log(form);
                        this.$message.success('提交成功！');
                        // alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {//重置
                this.$refs[formName].resetFields();
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }


        }

    }
</script>
<style scoped>
    h1{
        text-align: center;
        margin: 30px 0;
    }
    p{
        line-height: 30px;
        margin-bottom: 10px;
        text-indent: 2em;
    }
    .logout{
        color: #409EFF;
    }

</style>